<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a :class="{active:params.sorter==='weight_desc'}" @click.prevent="changeSorter('weight_desc')" href="javascript:;">推荐</a>

      <a :class="{active:params.sorter!=='weight_desc'}" @click.prevent="changeSorter(null)" href="javascript:;">最新</a>

      <div class="logo"><img src="@/assets/logo.png" alt=""></div>

    </nav>
<!-- 循环 -->
<van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <ArticleItem v-for="item in articleList" :item="item" :key="item.id" />
    </van-list>
  </div>
</template>

<script>
import { getArticlesApi } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      loading: false, // 判断是否正在加载
      finished: false, // 判断是否加载完成
      // 请求接口数据的参数对象
      params: {
        current: 1,
        sorter: 'weight_desc'
      },
      articleList: []
    }
  },
  methods: {

    // 1.处理加载
    async onLoad () {
      // 2.1获取文章列表数据
      const { data } = await getArticlesApi(this.params)
      // this.articleList = data.rows
      this.articleList.push(...data.rows)
      // 2.2处理分页和loading
      this.params.current++
      this.loading = false
      // 2.3判断是否加载完毕
      if (this.params.current > data.pageTotal) {
        this.finished = true
      }
    },
    // 2.切换
    changeSorter (sorter) {
      // 2.1重置数据
      this.params.current = 1
      this.articleList = []
      this.finished = false
      this.params.sorter = sorter

      // 2.2重新加载数据
      this.loading = true
      this.onLoad()
    }
  }

}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;

  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;

    >a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;

      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }

      &.active {
        color: #222;

        &::after {
          width: 14px;
        }
      }
    }

    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;

      >img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
